function showClassDialog(){
    var classDialog = document.getElementsByClassName("create-border")[0];
    classDialog.style.display="block";
}
function createCancel(){
    var classDialog = document.getElementsByClassName("create-border")[0];
    classDialog.style.display="none";
}
//创建班级
function createClass(){
    var className = document.getElementById("class-name-input").value;
    var description = document.getElementById("class-desc-input").value;
    if(className!=""){
        let formData = new FormData();
        formData.append('className', className);
        formData.append('description', description);
        fetch('/createClass', {
            method: 'post',
            body: formData,
        }).then(response => response.text()).then(data => {
            if (data == "success") {
                window.location.reload();
            } else if(data == "needLogin"){
                alert("需要登录");
            }else {
                alert("课堂创建失败!");
                return;
            }
        }).catch(function (e) {
            alert("error:" + e);
            return;
        })
    }else{
        alert("班级名称不能为空");
    }
}
//点击进入
function enterClass(element){
    classId = element.nextElementSibling.value;
    if (classId != "") {
        window.location.href="/class-details?classId="+classId;
    }
}
//展示填写邀请码的文本框
function showFillCodeBox(element){
    var fillCodeBox = document.getElementsByClassName("fill-code-border")[0];
    var classId = element.nextElementSibling.value;
    document.getElementById("class-id").value=classId;//传递classId
    fillCodeBox.style.display="block";
}
//填写邀请码函数
function fillCode(element){
    var code = element.previousElementSibling.value;
    var classId = document.getElementById("class-id").value;
    if(code!=""){
        fetch('/fillCode?classId=' + classId+'&code='+code)
        .then(function (res) {
            return res.text();
        })
        .then(function (data) {
            if (data == "success") {
                alert("成功加入班级");
                window.location.reload();
            }else if(data == "needLogin"){
                alert("请先登录");
                window.location.href="/login";
            }else if(data == "used"){
                alert("该邀请码已失效");
            }else if(data == "wrong"){
                alert("邀请码不存在");
            }else{
                alert("未知错误");
            }
        }).catch(function (e) {
            alert("error:" + e);
        })
    }else{
        alert("邀请码不能为空");
    }
}
//取消填写邀请码
function cancelFillCode(element){
    var fillCodeBox = document.getElementsByClassName("fill-code-border")[0];
    fillCodeBox.style.display="none";
    element.previousElementSibling.previousElementSibling.value="";
    document.getElementById("class-id").value="";
}